package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/10/10
 * @description
 * 日志的打印级别
 *  debug < info(默认)<  warning< error
 *  log.debug("hello")
 *  log.info("hello")
 *
 *  Restful 风格
 *  /order/add
 *  /order/delete?id=12
 *  /order/update
 *  /order/select?id=12
 *
 *    /order/12    get       查询      @GetMapping
 *    /order       post      添加操作  @PostMapping
 *    /order/12    delete   删除操作   @DeleteMapping
 *    /order       put      修改       @PutMapping
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("m1")
    public String  method1(){

//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }


        return "OrderService ok  "+port;
    }

    //根据订单编号 返回订单信息
    @RequestMapping("m2")
    public String  method2(Integer id){

        log.info("id={},port={}",id,port);

        return "OrderService ok  "+port;
    }

    //根据订单编号 返回订单信息  Restful
    @RequestMapping("m3/{id}")
    public String  method3(@PathVariable("id") Integer id){

        log.info("id={},port={}",id,port);
        return "OrderService ok  "+port;
    }


    // 保存订单信息
    @RequestMapping("m4")
    public String  method4(Order order){

        log.info("order={},port={}",order,port);

        return "OrderService ok  "+port;
    }

    // 保存订单信息
    @RequestMapping("m5")
    public String  method5(@RequestBody Order order){

        log.info("order={},port={}",order,port);

        return "OrderService ok  "+port;
    }

    //批量删除   ?ids=1&ids=2&ids=3
    @RequestMapping("m6")
    public String  method6(Integer[] ids){

        log.info("ids={},port={}",ids,port);

        return "OrderService ok  "+port;
    }


    //根据订单编号 返回订单信息  Restful  order  ->json
    @RequestMapping("m7/{id}")
    public Order  method7(@PathVariable("id") Integer id){

        Order order = new Order();
        order.setId(id);
        order.setName("mate60");
        return order;

    }

    @RequestMapping("m8")
    public List<Order >  method8(){

        Order order = new Order();
        order.setId(1);
        order.setName("mate60");

        List<Order> list = Arrays.asList(order, order, order, order);
        return list;

    }

}
